package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.comment.Comment;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.pojo.user.User;
import com.qingcheng.service.comment.CommentService;
import com.qingcheng.service.goods.SkuService;
import com.qingcheng.service.user.UserService;
import org.springframework.web.bind.annotation.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.comment.Comment;
import com.qingcheng.pojo.comment.MerchantResponse;
import com.qingcheng.pojo.comment.ReplyMerchant;
import com.qingcheng.service.comment.CommentService;
import com.qingcheng.service.comment.ReplyMerchantService;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/comment")
public class CommentController {

    @Reference
    private CommentService commentService;

    @Reference
    private UserService userService;
    @Reference
    private SkuService skuService;

    @PostMapping("/isShield")
    public Result isShield(@RequestBody Comment comment){
        System.out.println(comment);
        commentService.isShield(comment.getId(),comment.getIsShield());
        return new Result();
    }
    @Reference
    private ReplyMerchantService replyMerchantService;


    /**
     * @Author lbb
     * @param
     */
    @PostMapping("/replyByMerchant")
    public void replyByMerchant(Integer commentId, @RequestBody String content){
        System.out.println(commentId);
        System.out.println(content);
        ReplyMerchant replyMerchant = new ReplyMerchant();
        replyMerchant.setContent(content);
        replyMerchant.setCommentId(commentId);
        replyMerchant.setUsername("admin");
        replyMerchantService.replyMerchant(replyMerchant);
    }


    /**
     * @Author lbb
     * @param commentIdStr
     * 根据评论Id评论详情和商家回复
     */

    @GetMapping("/findMerchantById")
    public MerchantResponse findMerchantById(String commentIdStr){
        int commentId = Integer.parseInt(commentIdStr);
        Comment comment = commentService.findById(commentId);
        String username = comment.getUsername();
        String skuId = comment.getSkuId();
        Sku sku = skuService.findById(skuId);
        User user = userService.findById(username);
        ReplyMerchant replyMerchant = replyMerchantService.findByCommentId(commentId);
        MerchantResponse merchantResponse = new MerchantResponse();
        merchantResponse.setComment(comment);
        merchantResponse.setReplyMerchant(replyMerchant);
        merchantResponse.setUser(user);
        merchantResponse.setSku(sku);
        return merchantResponse;
    }

}
